
export function tranListToTreeData(list, id, pid) {
  id = !id ? 'id' : id;
  pid = !pid ? 'pid' : pid;
  const treeList = []
  const map = {}
  list.forEach(item => {
    if (!item.children) {
      item.children = []
    }
    map[item[id]] = item
  })

  list.forEach(item => {
    const parent = map[item[pid]]
    if (parent) {
      parent.children.push(item);
    } else {
      treeList.push(item)
    }
  })
  return treeList
}

export function treeDataToTranList(tree){
  const result = []
  if (!tree || tree.length === 0) return [];
  function traverse(nodes){
    for (let node of nodes){
      // if (!node.children || node.children.length === 0) {
      //   result.push(node)
      //   if (node.children) delete node.children
      // }else {
      //   traverse(node.children)
      // }
      result.push(node)
      if (node.children && node.children.length > 0){
        traverse(node.children)
      }
    }
  }
  traverse(JSON.parse(JSON.stringify(tree)))
  result.forEach(item => {
    if (item.children) delete item.children
  })
  return result;
}
// 查找当前节点的所有父节点
export function findParents(tree, targetId, key) {
  let parents = []
  function traverse(node, parentIds) {
    if (node[key] === targetId) {
      parents = [...parentIds]
      return true;
    }
    if (node.children) {
      for (let child of node.children) {
        if (traverse(child, [...parentIds, node[key]])) {
          return true;
        }
      }
    }
    return false;
  }
  for (let root of tree) {
    if (traverse(root, [])) {
      break;
    }
  }
  return parents;
}
// 通过指定字段去重
export function deduplicateByField(array, field){
  const seen = new Set();
  return array.filter(item => {
    const key = item[field];
    if (seen.has(key)){
      return false;
    }else {
      seen.add(key);
      return true;
    }
  })
}
// 通过指定字段和值查询所有符合条件的数据
export function findNodesByVal(data, key, val){
  let result = [];
  function recursiveSearch(nodes){
    for (const node of nodes){
      if (node[key] === val){
        result.push(node);
      }
      if (node.children && node.children.length > 0){
        recursiveSearch(node.children);
      }
    }
  }
  recursiveSearch(data)
  return result;
}

// 获取未来7天的日期
export function getDutureDate(time) {
  let dateArray = [];
  let timestamp = new Date(time).getTime();
  for (let i = 0; i < 7; i++) {
    const date = new Date(timestamp + i * 24 * 60 * 60 * 1000);
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
    const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
    dateArray.push(`${year}-${month}-${day}`)
  }
  return dateArray;
}
// 获取当前年月日
export function getNowDate() {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return `${year}-${month}-${day}`
}
// 计算某一天是星期几
export function getWeekDay(date) {
  const weekArray = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  const week = weekArray[new Date(date).getDay()];
  return week
}
// 计算N天后的日期
export function getBeforeDate(count) {
  let dd = new Date();
  dd.setDate(dd.getDate() + count);
  let y = dd.getFullYear();
  let m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
  let d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  return y + "-" + m + "-" + d;
}
export function uuid() {
  var s = []
  var hexDigits = "0123456789abcdef"
  for (var i = 0; i < 36; i++){
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10),1)
  }
  s[14] = "4"
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
  s[8] = s[13] = s[18] = s[23] = "-"

  var uuid = s.join("")
  uuid = uuid.split("-").join("")
  return uuid
}

export function findLastChildren(arr) {
  let result = []
  arr.forEach(item => {
    if (item.children && item.children.length > 0) {
      const lastChildren = findLastChildren(item.children)
      result = result.concat(lastChildren)
    } else {
      result.push(item)
    }
  });
  return result
}

export function findLeafNodes(node){
  if (!node.children || node.children.length === 0){
    return [node]
  }
  let leaves = []
  for (const child of node.children){
    leaves = leaves.concat(findLeafNodes(child));
  }
  return leaves;
}

